﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using C1.Olap;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace LightSwitchApplication.API
{
    public class OlapController : ApiController
    {
        public object Get()
        {
            using (var context = ServerApplicationContext.CreateContext())
            {
                return context.Application.User.FullName;
            }
        }

        public object Get(string row, string column, string value, string totals)
        {
            using (var context = ServerApplicationContext.CreateContext())
            {
                C1OlapEngine engine = new C1OlapEngine();
                engine.ShowKeyColumns = true;

                switch (totals)
                {
                    case "rows":
                        engine.ShowTotalsRows = ShowTotals.GrandTotals;
                        engine.ShowTotalsColumns = ShowTotals.None;
                        break;

                    case "columns":
                        engine.ShowTotalsRows = ShowTotals.None;
                        engine.ShowTotalsColumns = ShowTotals.GrandTotals;
                        break;

                    case "all":
                        engine.ShowTotalsRows = ShowTotals.GrandTotals;
                        engine.ShowTotalsColumns = ShowTotals.GrandTotals;
                        break;

                    case "none":
                        default:
                        engine.ShowTotalsRows = ShowTotals.None;
                        engine.ShowTotalsColumns = ShowTotals.None;
                        break;
                }

                // TODO: Replace the data source and collection names with actual entities from your DataWorkspace
                engine.DataSource = context.DataWorkspace.NorthwindData.Invoices.GetQuery().Execute();

                if (row != null)
                {
                    engine.RowFields.Add(new string[] { row });
                }

                if (column != null)
                {
                    engine.ColumnFields.Add(new string[] { column });
                }

                if (value != null)
                {
                    engine.ValueFields.Add(new string[] { value });
                }

                engine.Update();

                foreach (DataColumn c in engine.OlapTable.Columns)
                {
                    C1OlapKey key = engine.GetOlapKey(c);

                    if (key != null)
                    {
                        string name = key.GetFormattedValue(0);

                        if (!string.IsNullOrEmpty(name))
                        {
                            c.ColumnName = name;
                        }
                        else if (engine.ShowTotalsColumns == ShowTotals.None)
                        {
                            c.ColumnName = key.ValueField.Caption;
                        }
                        else
                        {
                            c.ColumnName = "Total";
                        }
                    }
                }

                if (engine.ShowTotalsRows != ShowTotals.None)
                {
                    foreach (DataRow r in engine.OlapTable.Rows)
                    {
                        C1OlapKey key = engine.GetOlapKey(r);

                        if (key.Values.Length == 0)
                        {
                            r[0] = "Total";
                        }
                    }
                }

                return JsonConvert.SerializeObject(engine.OlapTable, new DataTableConverter());
            }
        }
    }
}